﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Entity;
using Runda.EShop.Bll;
using Runda.EShop.Common.Utility;
using System.Data;

namespace Runda.EShop.SaaS.CustomerManage
{
    public partial class CustomerProduct : BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }


        public Guid CustomerID
        {
            get
            {
                if (Request.QueryString["CustomerID"] != null)
                {
                    return new Guid(Request.QueryString["CustomerID"]);
                }
                return Guid.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CustomerID == Guid.Empty) return;

                BizCustomerEntity entity = new CustomerBll().Get<BizCustomerEntity>(CustomerID);
                BizEnterpriseEntity entityEnt = new BizEnterpriseBll().Get<BizEnterpriseEntity>(entity.EntID);
                ltlCustomerName.Text = entityEnt.EntName+"-"+ entity.CustomerName;
                ltlCustomerCode.Text = entity.CustomerCode;
                ltlDeptName.Text = entity.DeptName;
                

                BindGrid(ConstHelper.PAGE_INIT_INDEX);
            }
        }

        private void BindGrid(int PageIndex)
        {
            int RecordCount = 0;
            DataTable dt = new RelationCustomerProductBll().GetProductListByCustomer(CustomerID, "ProductCode DESC", AspNetPager1.PageSize, PageIndex, out RecordCount);

            gvProductList.DataSource = dt;
            gvProductList.DataBind();

            AspNetPager1.RecordCount = RecordCount;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid(AspNetPager1.CurrentPageIndex - 1);
        }

        protected void btnRefersh_Click(object sender, EventArgs e)
        {
            BindGrid(AspNetPager1.CurrentPageIndex - 1);
        }

        protected void gvProductList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Guid ProductID = new Guid(gvProductList.DataKeys[e.RowIndex]["ProductID"].ToString());
            RelationCustomerProductBll bll = new RelationCustomerProductBll();
            bll.Delete(CustomerID, ProductID);

            BindGrid(ConstHelper.PAGE_INIT_INDEX);
        }
    }
}